from rest_framework import serializers

from .models import Category, CategoryImage, CategoryJson


class CategoryJsonSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        category_id = self.context["category_id"]
        return CategoryJson.objects.create(category_id=category_id, **validated_data)

    class Meta:
        model = CategoryJson
        fields = ["id", "json"]


class CategoryImageSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        category_id = self.context["category_id"]
        return CategoryImage.objects.create(category_id=category_id, **validated_data)

    class Meta:
        model = CategoryImage
        fields = ["id", "image"]


class CategorySerializer(serializers.ModelSerializer):
    images = CategoryImageSerializer(many=True, read_only=True)
    json_files = CategoryJsonSerializer(many=True, read_only=True)

    class Meta:
        model = Category
        fields = ["id", "name", "category_type", "remark", "creator", "images", 'json_files']
